<!DOCTYPE html>
<html>

<head lang="en">
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyComment.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <!-- 引入jquery 和 common.js 可以获取到地址栏上拼接的参数 -->
    <script type="text/javascript" src="/js/common.js"></script>
    <!-- 如果到底了就提示用户到底了的消息插件-->
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script>
        $(function () {
            var user = JSON.parse(sessionStorage.getItem("user"));
            var likeNum;
            var params = getParams(); // 获取地址栏上的数据信息转成key=value的形式,调用方法
            if (params.id) {
                //根据评论的id获取评论信息
                $.get("/strategieLikes/" + params.id + "/comment", function (data) {
                    likeNum = data.likeNum;
                    $(".comments").renderValues(data);
                    $(".container").renderValues(data, {
                        // 就像渲染a标签一样的方式去渲染星星的数量
                        getStar: function (item, value) {
                            var temp = "";
                            // 实心的星星
                            for (var i = 0; i < value; i++) {
                                temp += "<i class=\"fa fa-star\"></i>"
                            }
                            // 空心的星星
                            for (var i = 0; i < (5 - value); i++) {
                                temp += "<i class=\"fa fa-star fa-star-o\"></i>"
                            }
                            $(item).html(temp);
                        },
                        getHref: getHref
                    });
                    // 获取所有点赞人的id,如果其中包含当前用户的id,就让点赞的按钮转变颜色
                    if (user) {
                        $.get("/strategieLikes/comment/" + params.id, function (data) {
                            if (data.success) {
                                $(".good").addClass("fa-thumbs-up");
                                $(".good").attr("data-state", 1);
                            }
                        })
                    }
                });
            }
            // 当前页变量  / 手机是滑动查询下一页的
            var currentPage = 1;
            // 总页数
            var pages;
            //存储游记的数组
            var travelArr = []; // js中数组是边长的,很方便做数组拼接
            function query() {
                // 获取该攻略评论下的子评论
                $.get("/strategieLikes/" + params.id + "/repliesComment", {currentPage: currentPage}, function (data) {
                    $(".comments").renderValues(data);
                    $.merge(travelArr, data.list);
                    var json = {
                        list: travelArr
                    }
                    var number = json.list.length;
                    $(".comment").renderValues(json, {
                        getHref: getHref
                    });
                    // 获取总页数
                    pages = data.pages;
                });
                // 每次查询加一页
                currentPage = currentPage + 1;
            }

            // 调用方法 默认查询第一页
            query();
            //给window绑定事件
            $(window).scroll(function () {
                if ($(window).height() + $(document).scrollTop() + 1 >= $(document).height()) {
                    // console.log("进行分页....");
                    // 判断是否到最后一页 / 如果大于最后一页就不发送请求了
                    if (currentPage <= pages) {
                        query();
                    } else {
                        // 已经到最后了就提示用户已经到底了(引入插件)
                        $(document).dialog({
                            type: "notice",  // 好像时设置样式
                            content: "<span class='info-text'>没有更多了!</span>", // 显示的文字
                            autoClose: 1000 // 多久后关闭
                        })
                    }
                }
            })
            // 点击添加评论框
            $(".search").click(function () {
                if (user) {
                    $("#travelModal").removeClass("modal");
                    $("#travelModal").addClass("operationBottom");
                } else {
                    $(document).dialog({
                        type : 'confirm',
                        closeBtnShow: true,
                        content: '亲,你还未登陆!是否现在登陆?',
                        onClickConfirmBtn: function(){
                            window.location.href="/index.html";
                        }
                    });
                }
            });


            // 提交评论
            $("#submitBtn").click(function () {
                // 获取自定义标签内容
                // 打包成参数
                var data = {
                    content: $("[name='content']").val()//获取内容
                };
                // 提交数据
                $.post("/strategies/" + params.id + "/replyComment", data, function (data) {
                    // window.location.href = "/strategyCatalogs.html?id=" + 2;
                    window.location.reload();
                });
                $("#travelModal").removeClass("operationBottom");
                $("#travelModal").addClass("modal");
            });
            //取消取消评论按钮
            $("#remove").click(function () {
                $("#travelModal").removeClass("operationBottom");
                $("#travelModal").addClass("modal");
            });
            // 点赞功能

            $(".good").click(function () {
                if (user) {
                    var state = $(this).data("state");
                    if (state == 0) {
                        $(".good").addClass("fa-thumbs-up");
                        $(".good").data("state", 1);
                        $.post("/strategieLikes/" + params.id + "/likeComment", {state: state}, function (data) {
                            if (data.success) {
                                $(document).dialog({
                                    type: 'notice',
                                    content: "<span class='info-text'>点赞成功!</span>", // 显示的文字
                                    autoClose: 1500
                                });
                                likeNum += 1
                                $(".likenum").html(likeNum);
                            }
                        });
                    } else {
                        $(".good").removeClass("fa-thumbs-up");
                        $(".good").data("state", 0);
                        $.ajax({
                            url: "/strategieLikes/comment/" + params.id, /*请求路径*/
                            type: "delete", /*修改请求方式delete*/
                            success: function () {
                                $(document).dialog({
                                    type: 'notice',
                                    content: "<span class='info-text'>取消点赞成功!</span>", // 显示的文字
                                    autoClose: 1500
                                });
                                likeNum -= 1
                                $(".likenum").html(likeNum);
                            }
                        })
                    }
                } else {
                    $(document).dialog({
                        type : 'confirm',
                        closeBtnShow: true,
                        content: '亲,你还未登陆!是否现在登陆?',
                        onClickConfirmBtn: function(){
                            window.location.href="/index.html";
                        }
                    });
                }
            });


        });
    </script>
</head>

<body>
<div class="search-head operationTop">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:history.go(-1);">
                <!--<span><i class="fa fa-chevron-left fa-2x"></i></span>-->
                <span><i class="fa fa-chevron-left"></i></span>
            </a>
        </div>
        <div class="col">
            <span>点评详情</span>
        </div>
        <div class="col"></div>
    </div>
</div>
<div class="search-head" style="height: 48px;background: white;">
</div>
<div>

    <div class="container">
        <div class="row">
            <div class="col-2 comment-head">
                <a href="userProfiles.html" data-id="43">
                    <img class="rounded-circle" render-src="user.headImgUrl">
                </a>
                <!--<button class="btn" id="followBtn"><i class="fa fa-hand-o-right"> </i> 关注</button>-->
            </div>

            <div class="col">
                <p class="authorName"><span render-html="user.nickName"></span></p>
                <span class="comment-star" render-key="starNum" render-fun="getStar">

				</span>
                <span class="comment-date" render-html="createTime"></span>
                <div class="comment-content">
                    <p render-html="content">

                    </p>
                </div>
                <ul class="comment-img" render-loop="imgArr">
                    <li>
                        <img render-src="imgArr.self">
                    </li>

                </ul>

                <div class="comment-link">
                    <a render-fun="getHref" render-key="strategy.id" data-href="/strategyCatalogs.html?id=">
                        <img render-src="strategy.coverUrl">
                        <span render-html="strategy.title">
                        </span>
                        <i class="fa fa-angle-right fa-2x"></i>
                    </a>
                </div>

            </div>
        </div>
    </div>

    <div class="comments count d-flex justify-content-between">
        <div class="p-2">评论<span render-html="total">0</span></div>
        <div class="p-2">赞<span class="likenum" render-html="likeNum">0</span></div>
    </div>

</div>

<div class="comment">
    <div class="container" render-loop="list">
        <div class="row">
            <div class="col-2 comment-head">
                <a href="userProfiles.html" data-id="43">
                    <img class="rounded-circle" render-src="list.user.headImgUrl">
                </a>
            </div>
            <div class="col">
                <p class="authorName"><span render-html="list.user.nickName"></span></p>
                <span class="comment-date" render-html="list.createTime"></span>
                <div class="comment-content">
                    <p render-html="list.content">
                    </p>
                </div>
                <div class="d-flex icon">
                    <a render-fun="getHref" render-key="list.id" data-href="/replyComment.html?id=">
                        <i class="fa  fa-commenting-o" style="margin-right: 10px">
                            <i render-html="list.commentNums"></i>&nbsp评论
                        </i>
                        <i class="fa fa-thumbs-o-up dian"> <span render-html="list.likeNum"></span> </i>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>


<div style="height:45px;">
</div>
<div class="operation">
    <div class="d-flex justify-content-between">
        <div class="col-10" style="margin-top: 5px;margin-left: 0px;">
            <div class="col-12">
                <div class="input-group-sm search" style="border: #d5d5d5 solid 0.1em;height: 30px;">
                    <span style="font-size: 16px;">添加你的评论</span>
                </div>
            </div>
        </div>
        <div class="focus">
            <i class="fa fa-thumbs-o-up good" data-state="0"></i>
        </div>
    </div>
</div>
<!--operationBottom-->
<div class="bg modal" id="travelModal" tabindex="-1" role="dialog">
    <div class="container tag" style="margin-top: 10px;">
        <textarea name="content" class="form-control" rows="5" placeholder="发表你的评价"></textarea>
        <div style="float: right;">
            <div id="remove"><span style="position: relative;left: 10px;">取消</span></div>
            <div id="submitBtn"><span style="position: relative;left: 10px;">发表</span></div>
        </div>
        <div style="height:45px;">
        </div>
    </div>
</div>

</body>

</html>